﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Newton.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-24 4:34 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;

namespace BOL.Algorithms.Optimization
{
    public sealed class Newton : RootFinderByGradientBase
    {
        #region Constructor

        public Newton(Func<double, double> function, Func<double, double> derivative, int numberOfIterations = 100, double tolerance = 1e-10, bool isVerbose = true)
            : base(function, derivative, numberOfIterations, tolerance, isVerbose) { }

        #endregion

        #region RootFinderByGradientBase overriden

        protected override void Expect(double initial)
        {
            Current = initial - Function(initial) / Derivative(initial);
            Value = Function(Current.Value);
        }

        protected override void Maximize(ref double initial)
        {
            if (Current.HasValue)
                initial = Current.Value;
        }

        #endregion
    }
}
